# Copyright (c) 2019 Ruslan Baratov
# All rights reserved.

cmake_minimum_required(VERSION 3.12)
project(Hunter)

include(ExternalProject)

include("@HUNTER_SELF@/cmake/Hunter")

include(hunter_add_package)
include(hunter_assert_not_empty_string)
include(hunter_status_debug)

hunter_status_debug("Scheme: url_sha1_pip")

foreach(dependency ${DEPENDS_ON_PACKAGES})
  hunter_add_package(${dependency})
endforeach()

hunter_add_package(hunter_venv)
find_package(hunter_venv CONFIG REQUIRED)

find_package(Python REQUIRED)
hunter_status_debug("Using Python: ${Python_EXECUTABLE}")
hunter_status_debug("Python_SITELIB: ${Python_SITELIB}")
hunter_status_debug("Root: ${HUNTER_VENV_ROOT}")

hunter_status_debug("'pip' install suffix: ${HUNTER_VENV_INSTALL_SUFFIX}")

# Check preconditions
hunter_assert_not_empty_string("@HUNTER_SELF@")
hunter_assert_not_empty_string("@HUNTER_EP_NAME@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_URL@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_SHA1@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_DOWNLOAD_DIR@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_SOURCE_DIR@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_INSTALL_PREFIX@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_LICENSE_DIR@")

string(COMPARE EQUAL "@HUNTER_PACKAGE_PROTECTED_SOURCES@" "" is_empty)
if(is_empty)
  set(http_credentials "")
else()
  set(
      http_credentials
      HTTP_USERNAME "@HUNTER_PACKAGE_HTTP_USERNAME@"
      HTTP_PASSWORD "@HUNTER_PACKAGE_HTTP_PASSWORD@"
  )
endif()

if("@HUNTER_PACKAGE_LOG_BUILD@")
  set(log_build 1)
else()
  set(log_build 0)
endif()

if("@HUNTER_PACKAGE_LOG_INSTALL@" OR "@HUNTER_SUPPRESS_LIST_OF_FILES@")
  set(log_install 1)
else()
  set(log_install 0)
endif()

# Introduce variables:
# * CMAKE_INSTALL_LIBDIR
# * CMAKE_INSTALL_BINDIR
# * CMAKE_INSTALL_INCLUDEDIR
include(GNUInstallDirs)

include(CMakePackageConfigHelpers)

set(PROJECT_NAME "@HUNTER_EP_NAME@")
set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake")

# Use variables:
#   - PROJECT_NAME
configure_package_config_file(
    "@HUNTER_SELF@/cmake/templates/pip_config.cmake.in"
    "${project_config}"
    INSTALL_DESTINATION "${config_install_dir}"
)

# We don't support verbose when building in MSVC as some pip packages (pip_numpy)
# run config test builds which are intended to fail, but cause MSVC to think the
# whole build has failed.
if(HUNTER_STATUS_DEBUG AND NOT MSVC)
  set(pip_verbose "-vvv")
else()
  set(pip_verbose "")
endif()

ExternalProject_Add(
    "@HUNTER_EP_NAME@"
    URL
    @HUNTER_PACKAGE_URL@
    URL_HASH
    SHA1=@HUNTER_PACKAGE_SHA1@
    ${http_credentials}
    DOWNLOAD_DIR
    "@HUNTER_PACKAGE_DOWNLOAD_DIR@"
    TLS_VERIFY
    "@HUNTER_TLS_VERIFY@"
    SOURCE_DIR
    "@HUNTER_PACKAGE_SOURCE_DIR@"
    INSTALL_DIR
    "@HUNTER_PACKAGE_INSTALL_PREFIX@"
        # not used, just avoid creating Install/<name> empty directory
    CONFIGURE_COMMAND
        "@CMAKE_COMMAND@" -E echo "Dummy configure step"
    BUILD_COMMAND
        "@CMAKE_COMMAND@"
        -E
        env
        --unset=__PYVENV_LAUNCHER__ # macOS problems
        ${Python_EXECUTABLE}
        -m
        pip
        install
        ${pip_verbose}
        --target
        "@HUNTER_PACKAGE_INSTALL_PREFIX@/${HUNTER_VENV_INSTALL_SUFFIX}"
        --no-cache-dir # Do not touch user's '~/.cache/pip'
        --no-deps # Dependencies managed by Hunter
        --isolated # Run pip in an isolated mode, ignoring environment variables and user configuration.
        "@HUNTER_PACKAGE_SOURCE_DIR@"
    INSTALL_COMMAND
        "@CMAKE_COMMAND@"
        -E
        make_directory
        "@HUNTER_PACKAGE_INSTALL_PREFIX@/${config_install_dir}"
    COMMAND
        "@CMAKE_COMMAND@"
        -E
        copy
        "${project_config}"
        "@HUNTER_PACKAGE_INSTALL_PREFIX@/${config_install_dir}"
    COMMAND
        # Copy license files
        "@CMAKE_COMMAND@"
        "-C@HUNTER_ARGS_FILE@" # for 'HUNTER_INSTALL_LICENSE_FILES'
        "-Dsrcdir=@HUNTER_PACKAGE_SOURCE_DIR@"
        "-Ddstdir=@HUNTER_PACKAGE_LICENSE_DIR@"
        -P
        "@HUNTER_SELF@/scripts/try-copy-license.cmake"
    LOG_BUILD ${log_build}
    LOG_INSTALL ${log_install}
)
